<?php
/**
  * controller for pagetemplate table
 * @package naanal
 */
class ClsVirtualfield extends ClsNaanalController
{
	/**
         * request
         * @package naanal
         * @var string 
         */
        public $request=null;
        /**
         * get
         * @package naanal
         * @var string 
         */
	public $get=null;
        /**
         * post
         * @package naanal
         * @var string 
         */
	public $post=null;
        /**
         * files
         * @package naanal
         * @var string 
         */
	public $files=null;
        /**
         * pdo
         * @package naanal
         * @var string 
         */
	public $pdo=null;
        /**
         * controlpanel
         * @package naanal
         * @var string 
         */
	public $controlpanel=null;
        /**
         * mode
         * @package naanal
         * @var string 
         */
	public $mode="create";
        
        private $template_content=false;
	
        /**
         * construct
         * @package naanal
         * @param string $controlpanel
         */
	function __construct()
	{
                parent::__contstruct();
	}
	/**
         * getRedirect
         * @package naanal
         * @return string
         */
	function getRedirect()
	{
		return $this->redirect;
	}
	/**
         * create
         * @package naanal
         * @return array
         */
	function create()
	{
		$this->mode="create";
		$obj= ClsWrapper::getInstance("reportfield");//new ClsWPagetemplate();
		return array("form"=>$obj);
	}
	/**
         * delete
         * @package naanal
         */
	function delete()
	{
		$this->mode="insert";
		$obj=ClsWrapper::getInstance("reportfield");
		$obj->setArray(ClsNaanalRequest::getInstance()->getAll());
		$obj->delete();
      	$this->redirect="{$this->urlModuleParam}=reportfield&{$this->urlActionParam}=list";
	}
	/**
         * insert
         * @package naanal
         */
	function insert()
	{       
		$this->mode="insert";
		$obj=ClsWrapper::getInstance("reportfield");
		$obj->set_name(ClsNaanalRequest::getInstance()->getData("name"));
                $obj->set_data(ClsNaanalRequest::getInstance()->getData("data"));
                $obj->set_sort(ClsNaanalRequest::getInstance()->getData("sort"));
		$obj->insert();
      	$this->redirect="{$this->urlModuleParam}=reportfield&{$this->urlActionParam}=list";
	}
	/**
         * update
         * @package naanal
         */
	function update()
	{
		$this->mode="update";
		$obj=ClsWrapper::getInstance("reportfield");
                if($this->template_content!==false)
                {
                    $this->set_template($this->template_content);
                }
		$obj->set_name(ClsNaanalRequest::getInstance()->getData("name"));
                $obj->set_data(ClsNaanalRequest::getInstance()->getData("data"));
                $obj->set_sort(ClsNaanalRequest::getInstance()->getData("sort"));
                $obj->set_id(ClsNaanalRequest::getInstance()->getData("id"));
		$obj->update();
	    $this->redirect="{$this->urlModuleParam}=reportfield&{$this->urlActionParam}=list";
	}
        public function upload()
        {
            $files=  ClsNaanalFiles::getInstance()->getAll();
            if($files && !empty($files["template"]["tmp_name"]))
            {
                $success=move_uploaded_file($files["template"]["tmp_name"], APP_PATH."/tmp/".$files["template"]["name"]);
                if(!$success)
                {
                    trace("Unable to move {$files["template"]["name"]}");
                }
                $this->template_content=file_get_contents(APP_PATH."/tmp/".$files["template"]["name"]);
                unlink(APP_PATH."/tmp/".$files["template"]["name"]);
            }
        }
	/**
         * edit
         * @package naanal
         * @return array
         */
	function edit()
	{
		$this->mode="edit";
		$obj=ClsWrapper::getInstance("reportfield");
		$obj->loadObject(ClsNaanalRequest::getInstance()->getData("id"));
		return array("form"=>$obj);
	}
	/**
         * show list
         * @package naanal
         * @return array
         */
	function show_list()
	{
		$this->mode="list";
		$sql="select * from `reportfield`";
                $pdo=  ClsNaanalPDO::getNamedInstance(getDynamicDatabase());
		$pdo->setQuery($sql);
		$arrObj=$pdo->getAllObject("ClsWReportfield");
		return array("list"=>$arrObj);	
	}
	/**
         * render
         * @package naanal
         * @return string
         */
	function render()
	{     
		$mode=$this->mode;
		if($mode=="list")
			return $this->show_list();
		else
			return $this->$mode();
	}
}
?>